<?php

class ArtController extends Zend_Controller_Action
{

    public function preDispatch()
    {
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			return $this->_helper->redirector('index', 'auth');
		}
		$this->view->identity = $auth->getIdentity();
    }

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $article = new Application_Model_DbTable_Article();
        $this->view->lista = $article->showArticle();
        
    }

    public function createAction()
    {
        $form = new Application_Form_Artykul();
        $form->submit->setLabel('Dodaj');
        $this->view->form = $form;
        
        if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$name = $form->getValue('name');
				$content = $form->getValue('content');
				$dzial = $form->getValue('dzial');
				$artykul = new Application_Model_DbTable_Article();
				$artykul->addArticle($name, $content, $dzial);
				$this->_helper->redirector('index');
			}
			else{
				$form->populate($formData);
			}
		}
    }

    public function deleteAction()
    {
        if($this->getRequest()->isPost()){
			
			$del = $this->getRequest()->getPost('del');
			if($del == 'Yes'){
				
				$id= $this->getRequest()->getPost('id');
				$artykul = new Application_Model_DbTable_Article();
				$artykul->deleteArticle($id);
				
			}
			$this->_helper->redirector('index');
		}
		else{
			$id = $this->_getParam('id',0);
			$artykul = new Application_Model_DbTable_Article();
			$this->view->artykul = $artykul->getArticle($id);
			
		}
    }

    public function editAction()
    {
        $form = new Application_Form_Artykul();
        $form->submit->setLabel('Zapisz');
        $this->view->form = $form;
        
        if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$id = (int)$form->getValue('id');
				$name = $form->getValue('name');
				$content = $form->getValue('content');
				$dzial = $form->getValue('dzial');
				$artykul = new Application_Model_DbTable_Article();
				$artykul->updateArticle($id, $name, $content, $dzial);
				
				$this->_helper->redirector('index');
			}
			else{
				$form->populate($formData);
			}
			
		}
		else{
			$id = $this->_getParam('id', 0);
			if($id > 0){
				$artykul = new Application_Model_DbTable_Article();
				$form->populate($artykul->getArticle($id));
			}
		}
    }


}

















